package com.singlemusic.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.singlemusic.utils.AESUtils;
import com.singlemusic.utils.FileUtils;

@Controller
@RequestMapping("/test02")
public class FileController {
	
	@ResponseBody
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public List<String> upload(@RequestParam("file")MultipartFile file, HttpServletRequest request){
		InputStream is = null;
		String filename = null;
		String path = request.getSession().getServletContext().getRealPath("music");
		try {
			is= file.getInputStream();
			filename = file.getOriginalFilename();
			System.out.println(filename);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean flag = FileUtils.saveFile(is, path, filename);
		List<String> list = new ArrayList<String>();
		if(flag=true){
			list.add(AESUtils.enCrypt("/"+filename));
			//此处插入数据库
		}else{
			list.add("文件已存在或添加文件出现异常，添加失败！！！");
		}
		//List<String> list = new ArrayList<String>();
		//list.add("add success");
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/load01",method=RequestMethod.POST)
	public List<String> upload01(){
		List<String> list = new ArrayList<String>();
		list.add("test");
		return list;
	}
}
